﻿/******************************************************************************* 
  Copyright (C), 1988-2020, zeno Tech. Co., Ltd.
  Description:  流媒体打包接口头文件
  Function List:
            
*******************************************************************************/
#ifndef SYS_PACK_H 
#define SYS_PACK_H  

#ifdef __cplusplus
extern "C" {
#endif

/*=============================================================================
                                 Includes                                  
==============================================================================*/


/*=============================================================================
                               Macro Definition
==============================================================================*/
#define	INPUT_MIN_DEC_DATA	(0x64)

                        
/*=============================================================================
                        Local Structures and Typedef 
==============================================================================*/
typedef enum
{
    PACK_VIDEO,
    PACK_AUDIO,
    PACK_WATER
}PACK_streamType_t;

typedef enum
{
    PACK_H264,
    PACK_H265,
    PACK_JPEG,
    PACK_MJPEG
}PACK_videoType_t;

typedef enum
{
    PACK_G711A,
    PACK_G711U,
    PACK_G726_MEDIA_16K,
    PACK_ADPCM,
    PACK_PCM, 
    PACK_AAC,
    PACK_G726_MEDIA_24K,
    PACK_G726_MEDIA_32K,
    PACK_G726_MEDIA_40K,
    PACK_G726_16K,
    PACK_G726_24K,
    PACK_G726_32K,
    PACK_G726_40K,
}PACK_audioType_t;

/*打包类型*/
typedef enum
{
    PACK_ZLAV,
    PACK_DHAV,
    PACK_RAW
}PACK_packType_t;

/*跳帧参考类型*/
typedef enum
{
    PACK_1FOLD_SKIP = 1,
    PACK_2FOLD_SKIP = 2,
    PACK_4FOLD_SKIP = 4
}PACK_frameSkipType_t;

typedef enum
{
    PACK_BASE_IDRSLICE = 0,                 //the Idr frame at Base layer
    PACK_BASE_REFTOIDR = 1,
    PACK_BASE_PSLICE_REFBYBASE,             //the P frame at Base layer, referenced by other frames at Base layer
    PACK_BASE_PSLICE_REFBYENHANCE,          //the P frame at Base layer, referenced by other frames at Enhance layer
    PACK_ENHANCE_PSLICE_REFBYENHANCE,       //the P frame at Enhance layer, referenced by other frames at Enhance layer
    PACK_ENHANCE_PSLICE_NOTFORREF,          //the P frame at Enhance layer ,not referenced
    PACK_ENHANCE_PSLICE_BUTT
} PACK_frameType_t;


typedef struct
{
    PACK_videoType_t videoType;
    unsigned int frameType;      /* 定义见PACK_frameType_t */
    unsigned int u32Width;
    unsigned int u32Height;
    unsigned int fps;           /* 帧率 */
    unsigned int relFrames;     /* 相对前一个I帧计数 */
    unsigned int refMode;       /* 跳帧参考模式
                                   0或255为原缺省值;
                                   以位标识，由低位到高位
                                   0位不用，
                                   1位表示时候支持1/2跳，
                                   2位表示是否支持1/4跳，
                                   3位表示是否支持1/6跳，
                                   4位表示是否支持1/8跳，*/
}PACK_videoInfo_t;

typedef struct
{
    PACK_audioType_t audioType;
}PACK_audioInfo_t;

typedef struct
{
    unsigned int waterMarkLen;/* 水印数据有效长度 */
    unsigned char waterMarkBuf[256];/* 水印数据缓存数组 */
}PACK_waterInfo_t;
 
typedef struct
{
    int rawLen;                 /*流媒体裸数据长度*/
    int ivsLen;                 /*智能帧数据长度*/
    int headLen;                /*流媒体帧头长度*/
    int tailLen;                /*流媒体帧尾长度*/
    unsigned char *pRawBuf;     /*流媒体裸数据指针*/
    unsigned char *pHeadBuf;    /*流媒体数据帧头指针*/
    unsigned char *pTailBuf;    /*流媒体数据帧尾指针*/
}PACK_dataInfo_t;

/*流媒体数据封包结构体*/
typedef struct
{
    PACK_streamType_t streamType;
    PACK_packType_t packType;       /*打包类型*/
    PACK_dataInfo_t dataInfo;
    unsigned int avSequence;        /*序列号*/
    unsigned int msTimes;           /*时间戳*/

    union
    {
        PACK_videoInfo_t videoInfo;
        PACK_audioInfo_t audioInfo;
        PACK_waterInfo_t waterInfo;
    };
}PACK_streamInfo_t;

typedef struct
{
    PACK_packType_t packType; /*封装类型*/
    int inputOffset;    /*一帧数据的偏移量*/
    int packOffset;     /*一包数据的位置偏移量*/
    int praseLen;       /*上一个包的剩余数据长度*/
    int rawDataLen;     /*一帧裸数据的长度*/
    int packLen;        /*一包数据的总长度*/
    char *pInBuf;       /*输入数据的指针*/
    char *pPutBuf;      /*解析好的一帧裸数据指针*/
    unsigned char praseData[INPUT_MIN_DEC_DATA];  /* 上一个包的剩余数据 */  
}PACK_decAudio_t;

/*流媒体数据解包结构体*/
typedef struct
{
    PACK_streamType_t streamType;

    union
    {
        PACK_decAudio_t decAudio;
    };
}PACK_decStreamInfo_t;

/*=============================================================================
                             extern  Variables                                  
==============================================================================*/


/*=============================================================================
                             Function Definition                                  
==============================================================================*/ 

/*******************************************************************************
  Function:     PACK_VideoRawData
  Description:  生成数据帧头和数据帧尾
  Input:        - pStreamInfo：打包流媒体数据PACK_streamInfo_t结构体指针 
  Output:       - 无
  Return:       0: 操作成功
                -1: 操作失败
  Others:       无
*******************************************************************************/
int PACK_StreamRawData(PACK_streamInfo_t *pStreamInfo);

/*******************************************************************************
  Function:     PACK_DecStreamData
  Description:  解析音频包
  Input:        - pStreamInfo：解包流媒体数据PACK_decStreamInfo_t结构体指针 
  Output:       - 无
  Return:       0: 当前数据包解析完成
                1: 当前数据包解析未完成，还需继续解析
  Others:       无
*******************************************************************************/
int PACK_DecStreamData(PACK_decStreamInfo_t *pStreamInfo);


#ifdef __cplusplus
}
#endif
#endif // SYS_PACK_H 


